<?php

class Change_image extends CI_Controller {
    
    function __construct() {
        parent::__construct();
		$this->load->library('form_validation');		
		$this->load->helper(array('form','url','codegen_helper'));
		$this->load->model('codegen_model','',TRUE);
	}	
	
	function index(){
		$this->manage();
	}

	function manage(){
        $this->load->library('table');
        $this->load->library('pagination');
        
        //paging
        $config['base_url'] = base_url().'change_image/manage/';
        $config['total_rows'] = $this->codegen_model->count('change_image');
        $config['per_page'] = 3;	
        $this->pagination->initialize($config); 	
        $this->data['images'] = $this->codegen_model->get('change_image','id,url_image,thutu,trangthai','',$config['per_page'],$this->uri->segment(3));
        $this->load->view('admin/change_image/change_image_list', $this->data); 
       //$this->template->load('content', 'change_image_list', $this->data); // if have template library , http://maestric.com/doc/php/codeigniter_template
    }
	
    function add(){        
        $this->load->library('form_validation');    
		$this->data['custom_error'] = '';
		
        if ($this->form_validation->run('change_image') == false)
        {
             $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);

        } else
        {   
             if (isset($_FILES)) {
                $name = $_FILES['url_image']['name'];
                if (!empty($name)) {
                    // Specify configuration for File 1
                    $config['upload_path'] = 'upload/img_background/';
                    
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '10000';
                    //                $config['max_width'] = '5000';
                    //                $config['max_height'] = '5000';
                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('url_image')) {
                        $data = $this->upload->data();
                    } else {
                        echo $this->upload->display_errors();
                    }
                }
            }
            $data = array(
                    'url_image' => $name,
					'thutu' => set_value('thutu'),
					'trangthai' => set_value('trangthai')
            );
           
			if ($this->codegen_model->add('change_image',$data) == TRUE)
			{
				//$this->data['custom_error'] = '<div class="form_ok"><p>Added</p></div>';
				// or redirect
				redirect(base_url().'index.php/change_image/manage/');
			}
			else
			{
				$this->data['custom_error'] = '<div class="form_error"><p>An Error Occured.</p></div>';

			}
		}		   
		$this->load->view('admin/change_image/change_image_add', $this->data);   
        //$this->template->load('content', 'change_image_add', $this->data);
    }	
    
    function edit(){        
        $this->load->library('form_validation');    
        $this->load->library('upload');    
        
		$this->data['custom_error'] = '';
		
        if ($this->form_validation->run('change_image') == false)
        {
             $this->data['custom_error'] = (validation_errors() ? '<div class="form_error">'.validation_errors().'</div>' : false);

        } else
        {       
            if (isset($_FILES)) {
                $name = $_FILES['url_image']['name'];
                if (!empty($name)) {
                    // Specify configuration for File 1
                    $config['upload_path'] = 'upload/img_background/';
                    
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size'] = '10000';
                    //                $config['max_width'] = '5000';
                    //                $config['max_height'] = '5000';
                    // Initialize config for File 1
                    $this->upload->initialize($config);

                    if ($this->upload->do_upload('url_image')) {
                        $data = $this->upload->data();
                    } else {
                        echo $this->upload->display_errors();
                    }
                }
            }
            if($name!=''){
            $data = array(
                    'url_image' => $name,
                    'thutu' => $this->input->post('thutu'),
                    'trangthai' => $this->input->post('trangthai')
            );
            }else{
                $data = array(
                    'thutu' => $this->input->post('thutu'),
                    'trangthai' => $this->input->post('trangthai')
                );
            }
           
			if ($this->codegen_model->edit('change_image',$data,'id',$this->input->post('id')) == TRUE)
			{
				redirect(base_url().'index.php/change_image/manage/');
			}
			else
			{
				$this->data['custom_error'] = '<div class="form_error"><p>An Error Occured</p></div>';

			}
		}

		$this->data['result'] = $this->codegen_model->get('change_image','id,url_image,thutu,trangthai','id = '.$this->uri->segment(3),NULL,NULL,true);
		
		$this->load->view('admin/change_image/change_image_edit', $this->data);		
        //$this->template->load('content', 'change_image_edit', $this->data);
    }
	
    function delete(){
            $ID =  $this->uri->segment(3);
            $this->codegen_model->delete('change_image','id',$ID);             
            redirect(base_url().'index.php/change_image/manage/');
    }
}

/* End of file change_image.php */
/* Location: ./system/application/controllers/change_image.php */